package com.yiquan.sms.utils;

import com.alibaba.fastjson.JSONObject;
import entity.StatusCode;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.yaml.snakeyaml.util.UriEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tyrone
 * @date 2021/12/24 21:59
 */
@Component
public class SmsUtils {

    @Value("${sms.appcode:83d072532964455e8e9f1d3ddc8aae6d}")
    private String appcode;

    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    private static String host = "https://dfsns.market.alicloudapi.com";
    private static String path = "/data/send_sms";

    /**
     * 发送验证码
     *
     * @param phone
     * @param code
     */
    public boolean sendSms(String phone, String code) {
        String host = "https://dxyzm.market.alicloudapi.com";
        String path = "/chuangxin/dxjk";
        String method = "POST";
        Map<String, String> postBody = null;
        String content = "【邑大人事管理系统】你的验证码是：" + code + "，3分钟内有效！";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> query = new HashMap<String, String>();
        query.put("content", content);
        query.put("mobile", phone);
        Map<String, String> body = new HashMap<>();
        Integer resultCode = null;
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, query, body);
            resultCode = response.getStatusLine().getStatusCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (resultCode != null && resultCode == StatusCode.REQUEST_SUCCESS) {
            return true;
        }
        return false;
    }

    /**
     * RestTemplate增加消息转换器
     *
     * @param builder
     * @return
     */
    public RestTemplate getRestTemplate(RestTemplateBuilder builder) {
        //先获取到converter列表
        List<HttpMessageConverter<?>> converters = builder.build().getMessageConverters();
        for (HttpMessageConverter<?> converter : converters) {
            //因为我们只想要jsonConverter支持对text/html的解析
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                try {
                    //先将原先支持的MediaType列表拷出
                    List<MediaType> mediaTypeList = new ArrayList<>(converter.getSupportedMediaTypes());
                    //加入对text/html的支持
                    mediaTypeList.add(MediaType.APPLICATION_FORM_URLENCODED);
//将已经加入了text/html的MediaType支持列表设置为其支持的媒体类型列表
                    ((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(mediaTypeList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return builder.build();
    }

    public boolean sendSmsAli(String phone, String code) {

        String method = "POST";
        String appcode = "83d072532964455e8e9f1d3ddc8aae6d";
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("content", "code:"+code);
        bodys.put("phone_number", phone);
        bodys.put("template_id", "TPL_09341");


        try {
            HttpResponse response = HttpUtil.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            return true;
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
